package com.ruoyi.develop.orderIronRecord.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.orderIronDetail.domain.OrderIronDetail;
import com.ruoyi.develop.orderIronDetail.mapper.OrderIronDetailMapper;
import com.ruoyi.export.service.impl.ExportServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.orderIronRecord.mapper.OrderIronRecordMapper;
import com.ruoyi.develop.orderIronRecord.domain.OrderIronRecord;
import com.ruoyi.develop.orderIronRecord.service.IOrderIronRecordService;

/**
 * 铁附件订单拆解记录Service业务层处理
 *
 * @author lmm
 * @date 2024-10-21
 */
@Service
public class OrderIronRecordServiceImpl implements IOrderIronRecordService {
    @Resource
    private OrderIronRecordMapper orderIronRecordMapper;

    @Resource
    private OrderIronDetailMapper orderIronDetailMapper;
    @Autowired
    private ExportServiceImpl exportService;

    /**
     * 查询铁附件订单拆解记录
     *
     * @param id 铁附件订单拆解记录主键
     * @return 铁附件订单拆解记录
     */
    @Override
    public OrderIronRecord selectById(String id) {
        return orderIronRecordMapper.selectById(id);
    }

    /**
     * 查询铁附件订单拆解记录列表
     *
     * @param entity 铁附件订单拆解记录
     * @return 铁附件订单拆解记录
     */
    @Override
    public List<OrderIronRecord> selectList(OrderIronRecord entity) {
        return orderIronRecordMapper.selectList(entity);
    }

    @Override
    public List<OrderIronRecord> selectListBuyRemain(OrderIronRecord entity) {
        return orderIronRecordMapper.selectListBuyRemain(entity);
    }


    /**
     * 新增铁附件订单拆解记录
     *
     * @param entity 铁附件订单拆解记录
     * @return 结果
     */
    @Override
    public int insert(OrderIronRecord entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        // 获取订单下的最大拆解编号
        String maxSplitCode =  orderIronRecordMapper.selectMaxSplitCode(entity);
        // 如果拆解编号为空，则从1开始 ，否则加1
        if (StringUtils.isBlank(maxSplitCode)) {
            entity.setSplitCode("1");
        }else{
            entity.setSplitCode(String.valueOf(Integer.parseInt(maxSplitCode) + 1));
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return orderIronRecordMapper.insert(entity);
    }

    /**
     * 修改铁附件订单拆解记录
     *
     * @param entity 铁附件订单拆解记录
     * @return 结果
     */
    @Override
    public int update(OrderIronRecord entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return orderIronRecordMapper.update(entity);
    }

    /**
     * 批量删除铁附件订单拆解记录
     *
     * @param ids 需要删除的铁附件订单拆解记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return orderIronRecordMapper.deleteByIds(ids);
    }

    /**
     * 删除铁附件订单拆解记录信息
     *
     * @param id 铁附件订单拆解记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return orderIronRecordMapper.deleteById(id);
    }

    @Override
    public void exportDetailList(HttpServletRequest request, HttpServletResponse response, OrderIronRecord entity) {
        entity = orderIronRecordMapper.selectById(entity.getId());
        OrderIronDetail orderIronDetail = new OrderIronDetail();
        orderIronDetail.setRecordId(entity.getId());
        List<OrderIronDetail> list = orderIronDetailMapper.selectList(orderIronDetail);
        String fileName = "拆分铁附件列表.xlsx";
        String templateName = "铁附件拆分明细导出模板.xlsx";
        Map<String, Object> params = new HashMap<>();
        params.put("list", list);
        params.put("record", entity);
        params.put("deliveryDate", DateUtils.formatDate(entity.getDeliveryDate(), "yyyy-MM-dd"));
        exportService.baseXlsxExport(fileName, params, templateName, request, response);
    }

    @Override
    public void deleteByProjectId(String projectId) {
        orderIronRecordMapper.deleteByProjectId(projectId);
    }


}
